package cn.edu.cjw.APMFP.ProductionTask.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import cn.edu.cjw.APMFP.Product.Servicve.ProductService;
import cn.edu.cjw.APMFP.Product.Servicve.ProductServiceImpl;
import cn.edu.cjw.APMFP.ProductionTask.DAO.TaskDAO;
import cn.edu.cjw.APMFP.ProductionTask.DAO.TaskDAOImpl;
import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;

public class TaskServiceImpl implements TaskService {

	TaskDAO tDao = new TaskDAOImpl();
	ProductService pService = new ProductServiceImpl();

	public boolean addTask(String TaskId, String founder, Date creationTime, String chargeMan, Date completionTime,
			String describe) throws Exception {

		if (tDao.searchTaskById(TaskId).judge()) {
			return false;
		} else {

			ProductionTask task = new ProductionTask(TaskId, founder, creationTime, chargeMan, completionTime,
					describe);

			return tDao.addAndUpdateTask(task);

		}
	}

	public boolean ExamineTask(ProductionTask task, HashMap<String, Long> product) throws Exception {

		HashMap<String, Boolean> percentage = new HashMap<String, Boolean>();

		HashMap<String, Long> material = new HashMap<String, Long>();

		Set<Entry<String, Long>> entrySet = product.entrySet();
		for (Entry<String, Long> entry : entrySet) {

			HashMap<Integer, String> workstep = pService.searchProductById(entry.getKey()).getWorkstep();

			Set<Entry<Integer, String>> entrySet2 = workstep.entrySet();
			for (Entry<Integer, String> entry2 : entrySet2) {

				percentage.put(entry2.getValue(), false);

			}

			HashMap<String, Long> mate = pService.searchProductById(entry.getKey()).getMaterial();
			Set<Entry<String, Long>> entrySet3 = mate.entrySet();
			for (Entry<String, Long> entry2 : entrySet3) {
				material.put(entry2.getKey(), entry2.getValue() * entry.getValue());
			}

		}

		task.setExamine(percentage, product, material);

		return tDao.addAndUpdateTask(task);
	}

	public boolean updateTaskchargeMan(ProductionTask task, String chargeMan) throws Exception {
		task.setChargeMan(chargeMan);
		return tDao.addAndUpdateTask(task);
	}

	public boolean updateTaskCompletionTime(ProductionTask task, Date time) throws Exception {
		task.setCompletionTime(time);
		return tDao.addAndUpdateTask(task);
	}

	public boolean updateTaskPercentage(ProductionTask task, HashMap<String, Boolean> percentage) throws Exception {
		task.setPercentage(percentage);
		return tDao.addAndUpdateTask(task);
	}

	public boolean updateTask(ProductionTask task, String chargeMan, Date time, HashMap<String, Boolean> percentage)
			throws Exception {
		task.setChargeMan(chargeMan);
		task.setCompletionTime(time);
		task.setPercentage(percentage);
		return tDao.addAndUpdateTask(task);
	}

	public boolean removeTask(ProductionTask task) throws Exception {
		return tDao.removeTask(task);
	}

	public ProductionTask searchTaskById(String TaskId) throws Exception {
		return tDao.searchTaskById(TaskId);
	}

	public ArrayList<ProductionTask> searchTaskByIng() throws Exception {
		return tDao.searchTaskByIng();
	}

	public ArrayList<ProductionTask> searchTaskByExamine() throws Exception {
		return tDao.searchTaskByExamine();
	}

	public ArrayList<ProductionTask> searchTaskComplete() throws Exception {
		return tDao.searchTaskComplete();
	}

	public ArrayList<ProductionTask> showAll() throws Exception {

		return tDao.showAll();
	}

}
